<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */

declare(strict_types=1);

namespace App\Controller\Cms;


use App\Annotation\AdminRequired;
use App\Annotation\ParamValid;
use App\Annotation\PermissionMeta;
use App\Annotation\PermissionModule;
use App\Controller\AbstractController;
use App\Request\Admin\DispatchPermissionsRequest;
use App\Request\Admin\GroupRequest;
use App\Request\Admin\ResetPasswordRequest;
use App\Service\AdminService;
use App\Service\GroupService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;

/**
 * @Controller(prefix="cms/admin")
 * @PermissionModule(value = "管理员")
 * Class AdminController
 * @package App\Controller\Cms
 */
class AdminController extends AbstractController
{

    /**
     * @Inject()
     * @var AdminService
     */
    private $adminService;

    /**
     * @Inject()
     * @var GroupService
     */
    private $groupService;


    /**
     * @AdminRequired
     * @GetMapping("permission")
     * @PermissionMeta(value = "查询所有可分配的权限", mount = false)
     */
    public function getAllPermissions(){
       return $this->adminService->getAllStructuralPermissions();
    }

    /**
     * @AdminRequired
     * @GetMapping("group/all")
     * @PermissionMeta(value = "查询所有权限组", mount = false)
     */
    public function getAllGroup(){
        return $this->adminService->getAllGroups();
    }

    /**
     * @AdminRequired
     * @GetMapping("group/{id}")
     * @PermissionMeta(value = "查询一个权限组及其权限", mount = false)
     * @ParamValid(value="id",rule="positive")
     * @param $id
     */
    public function getGroup($id){
        return $this->adminService->getGroup($id);
    }

    /**
     * @AdminRequired
     * @PostMapping("group")
     * @PermissionMeta(value = "新建权限组", mount = false)
     * @param GroupRequest $request
     * @return mixed
     */
    public function createGroup(GroupRequest $request){
        $params = $request->post();
        $this->adminService->createGroup($params);
        return success(201,'权限分组添加成功');
    }

    /**
     * @AdminRequired
     * @PutMapping("group/{id}")
     * @PermissionMeta(value = "更新一个权限组", mount = false)
     * @ParamValid(value="id",rule="positive")
     * @param $id
     * @param GroupRequest $request
     */
    public function updateGroup(GroupRequest $request,$id){
        $request->scene('update')->validateResolved();
        $params = $request->post();
        $this->groupService->updateGroup($id,$params);
        return success(201,'更新权限组成功',7);
    }

    /**
     * @AdminRequired
     * @DeleteMapping("group/{id}")
     * @PermissionMeta(value = "删除一个权限组", mount = false)
     * @param $id
     */
    public function deleteGroup($id){
        $this->groupService->deleteGroup($id);
        return success(201,'删除权限组成功',8);
    }

    /**
     * @AdminRequired
     * @GetMapping("users")
     * @ParamValid(
     *     value={"count","page","group_id"},
     *     rule={"integer|min:1|max:30","integer","positive"}
     *     )
     * @PermissionMeta(value = "查询所有用户", mount = false)
     * @param $count
     * @param $page
     * @param $groupId
     */
    public function getUsers(){
        $count = $this->request->input('count',10);
        $page = $this->request->input('page',0);
        $groupId = $this->request->input('group_id','');
        return $this->adminService->getUserPageByGroupId($groupId,(int)$count,(int)$page);
    }


    /**
     * @AdminRequired
     * @PutMapping("user/{id}")
     * @ParamValid(value={"group_ids","group_ids.*"},rule={"array","positive"})
     * @PermissionMeta(value = "管理员更新用户信息", mount = false)
     * @param $id
     */
    public function updateUser($id){
        $params = $this->request->post();
        $this->adminService->updateUser($id,$params);
        return success(201,'更新用户信息成功',6);
    }

    /**
     * @AdminRequired
     * @PutMapping("user/{id}/password")
     * @PermissionMeta(value="修改用户密码", mount=false)
     * @param ResetPasswordRequest $request
     * @param $id
     * @return mixed
     */
    public function changeUserPassword(ResetPasswordRequest $request,$id){
        $params = $request->post();
        $this->adminService->changeUserPassword($id,$params);
        return success(201,'密码修改成功',2);
    }

    /**
     * @AdminRequired
     * @DeleteMapping("user/{id}")
     * @PermissionMeta(value = "删除用户", mount = false)
     * @ParamValid(value="id",rule="positive")
     */
    public function deleteUser($id){
        $this->adminService->deleteUser($id);
        return success(201,'删除用户成功',5);
    }

    /**
     * @AdminRequired
     * @PostMapping("permission/dispatch/batch")
     * @PermissionMeta(value = "分配单个权限", mount = false)
     * @param DispatchPermissionsRequest $request
     */
    public function dispatchPermissions(DispatchPermissionsRequest $request){
        $params = $request->post();
        $this->adminService->dispatchPermissions($params);
        return success(201,'添加权限成功',9);
    }

    /**
     * @AdminRequired
     * @PostMapping("permission/remove")
     * @PermissionMeta(value = "删除多个权限", mount = false)
     * @param DispatchPermissionsRequest $request
     * @return mixed
     */
    public function removePermissions(DispatchPermissionsRequest $request){
        $params = $request->post();
        $this->adminService->removePermissions($params);
        return success(201,'删除权限成功',9);
    }
}